import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:xlsk/config/easyLoading.dart';
import 'package:xlsk/store/model/userInfo.dart';
import 'package:xlsk/utils/dimension.dart';
import 'package:xlsk/view/count.dart';
import 'package:xlsk/view/hudong/index.dart';
import 'package:xlsk/view/index.dart';
import 'package:xlsk/view/login/index.dart';
import 'package:xlsk/view/my/index.dart';
import 'package:xlsk/view/start.dart';

class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _homeState();
  }
}

class _homeState extends State<Home> {
  Widget buildBody = Start();
  num loginStatus = 0; //0:默认状态(显示默认屏，防止检查登入时的白屏),1:已登入,2:未登入(去登录页)

  @override
  void initState() {
    super.initState();
    checkLogin();
  }

  @override
  Widget build(BuildContext context) {
    return buildBody;
  }

  checkLogin() async{
    var status = await UserInfo().checkLogin(context);
    Widget view = buildBody;
    if(status){
      view = Body();
    }else{
      SetEasyLoading(displayDuration: 1200);
      EasyLoading.showToast('未登录');
      view = Login();
    }
    setState(() {
      buildBody = view;
    });
  }

  @override
  void dispose() {
    super.dispose();
  }
}

class Body extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return _body();
  }
}

class _body extends State<Body>{
  late List _tabBar;
  late num current;
  late Widget currentBody;
  num safeBottom = 0;

  @override
  void initState() {
    super.initState();

    _tabBar = [
      {
        'id':1,
        'iconPath':'assets/images/home.png',
        'selectIconPath':'assets/images/home-active.png',
        'title':'首页',
        'body':Index()
      },
      {
        'id':2,
        'iconPath':'assets/images/class.png',
        'selectIconPath':'assets/images/class-active.png',
        'title':'统计',
        'body':Count()
      },
      {
        'id':3,
        'iconPath':'assets/images/hudong.png',
        'selectIconPath':'assets/images/hudong-active.png',
        'title':'互动',
        'body':Hudong()
      },
      {
        'id':4,
        'iconPath':'assets/images/my.png',
        'selectIconPath':'assets/images/my-active.png',
        'title':'我的',
        'body':My()
      },
    ];
    current = 1;
    currentBody = Index();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomAppBar(
        child: _buildTabBar(),
      ),
      body: currentBody,
    );
  }

  Widget _buildTabBar(){

    List<Widget> view = [];
    for(var item in _tabBar){
      view.add(
          GestureDetector(
            child: Container(
              child: Column(
                children: [
                  Image.asset(current==item['id']?item['selectIconPath']:item['iconPath'],width: Dimension(context).toPx(28),height: Dimension(context).toPx(28)),
                  Text('${item['title']}',style: TextStyle(color: current==item['id']?Color.fromRGBO(22, 97, 255, 1):Color.fromRGBO(144, 149, 153, 1),fontSize: Dimension(context).toPx(10)))
                ],
              ),
              width: Dimension(context).toVw(25),
              height: Dimension(context).toPx(50),
            ),
            onTap: (){
              setState(() {
                current = item['id'];
                currentBody = item['body'];
              });
            },
            behavior: HitTestBehavior.opaque,
          )
      );
    }
    Widget content = Container(
      child: Column(
        children: [
          Container(
            child: Row(
              children: view,
            ),
            width: Dimension(context).toPx(375),
            height: Dimension(context).toPx(50),
          ),
          Container(
            height: Dimension(context).toPx(safeBottom), //安全底部
          )
        ],
      ),
      width: Dimension(context).toPx(375),
      height: Dimension(context).toPx(50+safeBottom),
    );
    return content;
  }

  @override
  void dispose() {
    super.dispose();
  }
}